package com.csd.serviceImpl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.csd.dao.ChangeDAO;
import com.csd.dao.ProjectDAO;
import com.csd.dao.TeacherDAO;
import com.csd.exception.*;
import com.csd.helper.IConstant;
import com.csd.helper.ProjectState;
import com.csd.helper.SessionManager;
import com.csd.helper.Tool;
import com.csd.pojo.ChangeRecord;
import com.csd.pojo.Project;
import com.csd.pojo.Teacher;
import com.csd.service.IChangeRecordService;
import com.csd.vo.ChangeRecordVO;

/**
 * 变更业务逻辑
 * 
 * @author sanmu，尹华彬 <br>
 *         创建于2009-12-17
 */
public class ChangeRecordService implements IChangeRecordService {

	private ChangeDAO changeDAO;
	private TeacherDAO teacherDAO;
	private ProjectDAO projectDAO;

	public TeacherDAO getTeacherDAO() {
		return teacherDAO;
	}

	public void setTeacherDAO(TeacherDAO teacherDAO) {
		this.teacherDAO = teacherDAO;
	}

	public ProjectDAO getProjectDAO() {
		return projectDAO;
	}

	public void setProjectDAO(ProjectDAO projectDAO) {
		this.projectDAO = projectDAO;
	}

	public ChangeDAO getChangeDAO() {
		return changeDAO;
	}

	public void setChangeDAO(ChangeDAO changeDAO) {
		this.changeDAO = changeDAO;
	}
	/**
	 * 删除项目变更
	 * 
	 * @param ID
	 *            变更id
	 * @return true为修改成功,false 为修改失败
	 */
	public boolean deleteProjectChange(Integer id) {
		ChangeRecord changeRecord = changeDAO.findById(id);
		try {
			changeDAO.delete(changeRecord);
		} catch (RuntimeException re) {
			throw new DeleteException(re, "删除工厂变更异常");
		}
		return true;
	}
	/**
	 * 通过ID获得项目变更
	 * 
	 * @param ID
	 *            变更id
	 * @return changeRecord为变更记录
	 */
	public ChangeRecord getChangeRecordByID(Integer id) {
		ChangeRecord changeRecord = null;
		try {
			changeRecord = changeDAO.findById(id);

		} catch (RuntimeException re) {
					throw new GetException(re, "查询变更异常");
				}
		return changeRecord;
	}

	/**
	 * 通过项目ID获得项目变更
	 * 
	 * @param ID
	 *            项目id
	 * @return list 为项目的变更列表
	 */
	public List<ChangeRecord> listProjectChange(Integer id) {
		List<ChangeRecord> changeRecords = null;
		try {
			changeRecords = changeDAO.findByProjectID(id);

		} catch (RuntimeException re) {
					throw new GetException(re, "列出工程所有变更异常");
				}
		return changeRecords;
	}

	/**
	 * 保存变更
	 * @param changeRecord
	 *        变更记录
	 * @return  为项目的变更列表
	 */
	public boolean saveProjectChange(ChangeRecord changeRecord) {
		boolean tag = false;
		try {
			int id = SessionManager.getUserID();
			List list = projectDAO.findByTeam(id);
			if (list != null && list.size() > 0) {
				Project project = (Project) list.get(0);
				if ((project.getState() != ProjectState.projectEnd)  
						|| (project.getState() != ProjectState.projectRejected)
						|| (project.getState() != ProjectState.prophaseReject)) 
				{
					changeRecord.setTime(Tool.getCurrentDate());
					changeRecord.setProject((Project) list.get(0));
					changeRecord.setState(IConstant.teacherUnhandle);
					changeRecord.setResult("-1");
					changeDAO.save(changeRecord);
					tag = true;
				}
			}

		} catch (RuntimeException re) {
				throw new SaveException(re, "变更提交异常");
			}

		return tag;
	}
	
	/**
	 * 获得老师需要审批的变更记录
	 * @param teacherID
	 *        老师ID
	 * @return changeRecords，为老师所要审批的变更
	 */
	public List<ChangeRecord> listChangeRecordByTeacher(Integer teacherID) {
		List<ChangeRecord> changeRecords = null;
		Teacher teacher = null;
		teacher = teacherDAO.findById(teacherID);
		try {
			changeRecords = changeDAO.listChangeRecordByTeacher(teacher);

		} catch (RuntimeException re) {
			throw new GetException(re, "列出老师所要审批的变更异常");
		}

		return changeRecords;
	}

	
	/**
	 * 更新变更记录
	 * @param id
	 *        老师ID
	 * @return changeRecords，为老师所要审批的变更
	 */
	public boolean updateChangeRecord(Integer id, String advice,
			String changeResult) {
		ChangeRecord changeRecord = changeDAO.findById(id);
		changeRecord.setOpinion(advice);
		if (changeResult.equals("1")) {
			changeRecord.setResult(IConstant.changeRecordPass);
		} else if (changeResult.equals("0")) {
			changeRecord.setResult(IConstant.changeRecordReject);
		} else {

		}
		changeRecord.setState(IConstant.managerUnhandle);
		try {
			changeDAO.updateProjectChange(changeRecord);
		} catch (RuntimeException re) {
			throw new UpdateException(re, "提交老师的意见和结果异常");
		}
		return true;
	}

	/**
	 * 删除变更记录
	 * @param  changRecord
	 *       变更实例
	 * @return true为修改成功,false 为修改失败
	 */
	public boolean updateProjectChange(ChangeRecord changRecord) {
		try {
			changeDAO.updateProjectChange(changRecord);
		} catch (RuntimeException re) {
			throw new UpdateException(re, "更新更变记录异常");
		}
		return true;
	}

	/**
	 * 管理员受理或驳回变更
	 * 
	 * @param handleResult
	 *            受理或驳回
	 * @param changeRecordID
	 *            受理的变更id
	 * @return 处理成功为1，否则为0
	 */
	public boolean changeRecoedHandleOfManager(String handleResult,
			Integer changeRecordID) {
		ChangeRecord changeRecord = null;
		boolean bl = false;

		try {
			changeRecord = changeDAO.findById(changeRecordID);
			if (changeRecord != null) {
				// 双重判断的原因是防止action传来的不是1或0
				if (handleResult.equals("1")) {
					changeRecord.setResult(IConstant.changeRecordPass);
					changeRecord.setState(IConstant.managerHandle);
					// System.out.println(handleResult+"=======1");
				} else if (handleResult.equals("0")) {
					changeRecord.setResult(IConstant.changeRecordReject);
					changeRecord.setState(IConstant.managerHandle);
					// System.out.println(handleResult+"=======0");
				}
				// System.out.println(handleResult+"=======10");

			}
			changeDAO.update(changeRecord);
			bl = true;
		} catch (RuntimeException re) {
			throw new UpdateException(re, "添加处理结果异常");
		}
		return bl;
	}

	/**
	 * 查询单条变更详情
	 * 
	 * @param changeRecordID
	 *            变更id
	 * @return 变更实例
	 */
	public ChangeRecordVO searchChangeRecord(Integer changeRecordID) {
		ChangeRecord cr = null;
		ChangeRecordVO temp = null;
		try {
			cr = changeDAO.findById(changeRecordID);
			if (cr != null) {
				temp = new ChangeRecordVO(cr.getProject().getId(), cr.getId(),
						cr.getProject().getProjectType().getName(), cr
								.getProject().getProjectname(), cr.getProject()
								.getTeam().getStudent().getName(), cr
								.getState(), cr.getTime(), cr.getOpinion(), cr
								.getContent(), cr.getResult(), cr.getBackup(),
						cr.getReason());
			}

		} catch (RuntimeException re) {
			throw new GetException(re, "查询变更记录异常");
		}
		return temp;
	}

	/**
	 * 查询管理员没有处理的变更
	 * 
	 * @return 没处理变更实例列表
	 */
	public List<ChangeRecordVO> listUnhandleCROfManager() {
		List<ChangeRecord> temp = null;
		List<ChangeRecordVO> changeRecordVO = new ArrayList<ChangeRecordVO>();
		try {
			temp = changeDAO.listUnhandleCROfManager();
			for (Iterator iterator = temp.iterator(); iterator.hasNext();) {
				ChangeRecord cr = (ChangeRecord) iterator.next();
				changeRecordVO.add(new ChangeRecordVO(cr.getProject().getId(),
						cr.getId(), cr.getProject().getProjectType().getName(),
						cr.getProject().getProjectname(), cr.getProject()
								.getTeam().getStudent().getName(), cr
								.getState(), cr.getTime(), cr.getOpinion(), cr
								.getContent(), cr.getResult(), cr.getBackup(),
						cr.getReason()));
			}

		} catch (RuntimeException re) {
			throw new GetException(re, "列出管理员未处理的变更 异常");
		}
		return changeRecordVO;
	}

	/**
	 * 查询管理员驳回的变更
	 * 
	 * @return 驳回的变更实例列表
	 */
	public List<ChangeRecordVO> listRejectCROfManager() {
		List<ChangeRecord> temp = null;
		List<ChangeRecordVO> changeRecordVO = new ArrayList<ChangeRecordVO>();
		try {
			temp = changeDAO.listRejectCROfManager();
			for (Iterator iterator = temp.iterator(); iterator.hasNext();) {
				ChangeRecord cr = (ChangeRecord) iterator.next();
				changeRecordVO.add(new ChangeRecordVO(cr.getProject().getId(),
						cr.getId(), cr.getProject().getProjectType().getName(),
						cr.getProject().getProjectname(), cr.getProject()
								.getTeam().getStudent().getName(), cr
								.getState(), cr.getTime(), cr.getOpinion(), cr
								.getContent(), cr.getResult(), cr.getBackup(),
						cr.getReason()));
			}

		} catch (RuntimeException re) {
			throw new GetException(re, "列出管理员驳回的变更异常");
		}
		return changeRecordVO;
	}

	/**
	 * 查询管理员通过的变更
	 * 
	 * @return 通过变更实例列表
	 */
	public List<ChangeRecordVO> listAcceptCROfManager() {
		List<ChangeRecord> temp = null;
		List<ChangeRecordVO> changeRecordVO = new ArrayList<ChangeRecordVO>();
		try {
			temp = changeDAO.listAcceptCROfManager();
			for (Iterator iterator = temp.iterator(); iterator.hasNext();) {
				ChangeRecord cr = (ChangeRecord) iterator.next();
				changeRecordVO.add(new ChangeRecordVO(cr.getProject().getId(),
						cr.getId(), cr.getProject().getProjectType().getName(),
						cr.getProject().getProjectname(), cr.getProject()
								.getTeam().getStudent().getName(), cr
								.getState(), cr.getTime(), cr.getOpinion(), cr
								.getContent(), cr.getResult(), cr.getBackup(),
						cr.getReason()));
			}

		} catch (RuntimeException re) {
			throw new GetException(re, "查询管理员通过的变更异常");
		}
		return changeRecordVO;
	}

	/**
	 * 查询项目对应的变更
	 * @param projectID
	 *        项目ID
	 * @return list 为变更的列表
	 */
	public List<ChangeRecord> listChangeRecordByProject(Integer projectID) {
		List<ChangeRecord> list = null;

		try {
			list = changeDAO.findByProperty("project.id", projectID);
		} catch (RuntimeException re) {
			re.printStackTrace();
		}

		return list;

	}
	/**
	 * 查询团队对应的变更
	 * @return list 为变更的列表
	 */
	public List<ChangeRecord> listChangeRecordByTeam() {
		List<ChangeRecord> list = new ArrayList<ChangeRecord>();

		try {
			int id = SessionManager.getUserID();
			id = id;
			List temp = projectDAO.findByTeam(id);
			if (temp != null) {
				if (temp.size() > 0) {
					Project project = (Project) temp.get(0);

					list = changeDAO.findByProperty("project", project);

				} else {
					list = new ArrayList();
				}
			}

		} catch (RuntimeException re) {
			throw new GetException(re, "查询团队的变更异常");
		}

		return list;
	}

}
